package com.example.zyfypt_206shj.activity;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.zyfypt_206shj.R;
import com.example.zyfypt_206shj.adapter.ArticleAdapter;
import com.example.zyfypt_206shj.adapter.AttentionAdapter;
import com.example.zyfypt_206shj.bean.ArticleBean;
import com.example.zyfypt_206shj.bean.AttentionBean;
import com.example.zyfypt_206shj.bean.CollectionBean;
import com.example.zyfypt_206shj.bean.ProjectBean;
import com.example.zyfypt_206shj.bean.SessionID;
import com.example.zyfypt_206shj.bean.TcaseBean;
import com.example.zyfypt_206shj.bean.TwareBean;
import com.example.zyfypt_206shj.bean.VideoBean;
import com.example.zyfypt_206shj.fragement.Fragment1;
import com.example.zyfypt_206shj.fragement.Fragment2;
import com.example.zyfypt_206shj.fragement.Fragment3;
import com.example.zyfypt_206shj.fragement.Fragment4;
import com.example.zyfypt_206shj.fragement.Fragment5;
import com.example.zyfypt_206shj.fragement.Fragment_collection;
import com.example.zyfypt_206shj.fragement.Fragment_project;
import com.example.zyfypt_206shj.fragement.Fragment_tcase;
import com.example.zyfypt_206shj.iface.CollectionListener;

import java.util.ArrayList;
import java.util.List;

public class activity_collection extends AppCompatActivity{

    private LinearLayout layout1,layout2,layout3,layout4,layout5;//用于界面切换
    private ImageView iv1,iv2,iv3,iv4,iv5;//用户设置图片
    private ViewPager vp;//用于滑动切换
    private List<Fragment> listfragment;//Fragment  list
    private FragmentPagerAdapter fpadapter;//用来呈现Fragment页面

    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection);
        init();//初始化界面控件，设置控件监听
        initFragment();//初始化Fragment相关
    }

    //初始化Fragment相关
    private void initFragment() {
        listfragment=new ArrayList<Fragment>();//实例化Fragment list
        //将Fragment加入到list中
        listfragment.add(new Fragment_collection("article"));
        listfragment.add(new Fragment_collection("tcase"));
        listfragment.add(new Fragment_collection("tware"));
        listfragment.add(new Fragment_collection("video"));
        listfragment.add(new Fragment_collection("project"));


        //实例化FragmentPagerAdapter
        fpadapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            //获取某个Fragment
            @Override
            public Fragment getItem(int position) {
                return listfragment.get(position);
            }
            //确定Fragment数量
            @Override
            public int getCount() {
                return listfragment.size();
            }
        };
        vp=(ViewPager)findViewById(R.id.viewPager);//实例化ViewPager
        vp.setAdapter(fpadapter);//绑定ViewPager和Adapter
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            //根据ViewPager的position选择某Fragment
            @Override
            public void onPageSelected(int position) {
                vp.setCurrentItem(position);
                resetImage();//底部图片重置
                selectTab(position);//选择某个Fragment
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
    //layout点击事件
    View.OnClickListener  onClickListener =new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //先将四个按钮都设成未选图片
            resetImage();
            //然后根据底部的选择切换Fragment和ViewPager
            switch (view.getId())
            {
                case R.id.layout1:
                    selectTab(0);
                    break;
                case R.id.layout2:
                    selectTab(1);
                    break;
                case R.id.layout3:
                    selectTab(2);
                    break;
                case R.id.layout4:
                    selectTab(3);
                    break;
                case R.id.layout5:
                    selectTab(4);
                    break;
//                case R.id.imageButton6:
//                    Toast.makeText(activity_main.this, "you click imageButton6", Toast.LENGTH_SHORT).show();
//                    selectTab(5);
//                    break;
            }
        }
    };

    private void init() {
        //实例化ImageButton为了切换图片
        iv1=(ImageView)findViewById(R.id.imageView1);
        iv2=(ImageView)findViewById(R.id.imageView2);
        iv3=(ImageView)findViewById(R.id.imageView3);
        iv4=(ImageView)findViewById(R.id.imageView4);
        iv5=(ImageView)findViewById(R.id.imageView5);
//        ib6=(ImageButton)findViewById(R.id.imageButton6);

        //实例化Layout为了底部栏点击事件切换页面
        layout1=(LinearLayout)findViewById(R.id.layout1);
        layout2=(LinearLayout)findViewById(R.id.layout2);
        layout3=(LinearLayout)findViewById(R.id.layout3);
        layout4=(LinearLayout)findViewById(R.id.layout4);
        layout5=(LinearLayout)findViewById(R.id.layout5);

        //layout点击事件
        layout1.setOnClickListener(onClickListener);
        layout2.setOnClickListener(onClickListener);
        layout3.setOnClickListener(onClickListener);
        layout4.setOnClickListener(onClickListener);
        layout5.setOnClickListener(onClickListener);
//        ib5.setOnClickListener(onClickListener);
//        ib6.setOnClickListener(onClickListener);
    }
    //重置底部栏图片为未选
    private  void resetImage()
    {
        iv1.setImageResource(R.drawable.article_normal);
        iv2.setImageResource(R.drawable.tcase_normal);
        iv3.setImageResource(R.drawable.tware_normal);
        iv4.setImageResource(R.drawable.video_normal);
        iv5.setImageResource(R.drawable.me_normal);
//        ib6.setImageResource(R.drawable.video_normal);
    }
    //切换Fragment和ViewPager
    private void selectTab(int i)
    {
        switch (i)
        {
            case 0:
                iv1.setImageResource(R.drawable.article_pressed);
                break;
            case 1:
                iv2.setImageResource(R.drawable.tcase_pressed);
                break;
            case 2:
                iv3.setImageResource(R.drawable.tware_pressed);
                break;
            case 3:
                iv4.setImageResource(R.drawable.video_pressed);
                break;
            case 4:
                iv5.setImageResource(R.drawable.me_pressed);
                break;
        }
        vp.setCurrentItem(i);
    }

}
